package com.hanggold.jfinal;

import com.hanggold.jfinal.directive.MyDirective;
import com.hanggold.jfinal.interceptor.OneDemoInterceptor;
import com.hanggold.jfinal.interceptor.TwoDemoInterceptor;
import com.hanggold.jfinal.model.UserInfo;
import com.hanggold.jfinal.plugin.DemoPlugin;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.hikaricp.HikariCpPlugin;
import com.jfinal.server.undertow.UndertowServer;
import com.jfinal.template.Engine;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class JfinalApplication extends JFinalConfig {
    
    @Override
    public void configConstant(Constants constants) {
        constants.setInjectDependency(true);
        constants.setInjectSuperClass(true);
    }
    
    /**
     * 配置路由
     *
     * @param routes
     */
    @Override
    public void configRoute(Routes routes) {
        routes.scan("com.hanggold");
        routes.addInterceptor(new OneDemoInterceptor());
    }
    
    @Override
    public void configEngine(Engine engine) {
        //        engine.setDevMode(true);
        engine.setToClassPathSourceFactory();
        engine.setBaseTemplatePath("/view");
        engine.addSharedFunction("/common/_layout.html");
        engine.addSharedFunction("/common/_paginate.html");
        engine.addDirective("my", MyDirective.class);
    
       
        
    
    }
    
    @Override
    public void configPlugin(Plugins plugins) {
        plugins.add(new DemoPlugin());
        HikariCpPlugin hikariCpPlugin = new HikariCpPlugin("jdbc:mysql://172.17.135.88:3306/jfinal?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true", "devth", "devth");
        plugins.add(hikariCpPlugin);
        ActiveRecordPlugin arp = new ActiveRecordPlugin(hikariCpPlugin);
        plugins.add(arp);
        arp.addMapping("user_info", UserInfo.class);
    }
    
    @Override
    public void configInterceptor(Interceptors interceptors) {
        interceptors.add(new TwoDemoInterceptor());
    }
    
    @Override
    public void configHandler(Handlers handlers) {
        
    }
    
    public static void main(String[] args) {
        UndertowServer.start(JfinalApplication.class, 8801, true);
        log.info("启动成功");
        
    }
}
